<?php
class InformesController extends AppController {

	var $name = 'Informes';
	var $helpers = array('Access');

	function index($mes=null, $year=null, $zona=null, $bimestre=null,$year_origen=null){
		$usuario_activo = $this->Access->getMy('username');
		//Verificacion de sesion del sistema		
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
		$this->Informe->recursive = 0;
		$informes=$this->Informe->find('all',array('conditions'=>array('Informe.mes'=>$year.'-'.$mes,'AND'=>array('Informe.zona_id'=>$zona))));
		$this->set('informes', $this->paginate());
		$group_id = $this->Access->getMy('group_id');
		$this->set(compact('zona','mes','year','bimestre','year_origen','group_id','informes'));
	}

	function view($id = null) {
		$usuario_activo = $this->Access->getMy('username');
		//Verificacion de sesion del sistema		
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
		if (!$id) {
			$this->Session->setFlash(__('Invalid informe', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('informe', $this->Informe->read(null, $id));
	}

	function add($mes=null, $year=null, $zona=null, $bimestre=null, $year_origen=null) {
		$usuario_activo = $this->Access->getMy('username');
		//Verificacion de sesion del sistema		
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
		if (!empty($this->data)) {
			$form=$this->data;			
			$this->Informe->create();
			if ($this->Informe->save($this->data)) {
				$this->Session->setFlash(__('The informe has been saved', true));
				$this->redirect(array('action' => 'index', $form['Informe']['mes2'], $form['Informe']['year'], $form['Informe']['zona_id'],$form['Informe']['bimestre'], $form['Informe']['year_origen']));
			} else {
				$this->Session->setFlash(__('The informe could not be saved. Please, try again.', true));
			}
		}
		$mes2=$year.'-'.$mes;
		$this->set(compact('zona','mes','year','mes2', 'bimestre', 'year_origen'));
	}

	function edit($id = null, $mes=null, $year=null, $zona=null, $bimestre=null, $year_origen=null) {
		$usuario_activo = $this->Access->getMy('username');
		//Verificacion de sesion del sistema		
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
		if (!$id && !$mes && !$year && !$zona && empty($this->data)) {
			$this->Session->setFlash(__('Invalid informe', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$form=$this->data;	
			//die(var_dump($form));
			if ($this->Informe->save($this->data)) {
				$this->Session->setFlash(__('The informe has been saved', true));
				$this->redirect(array('action' => 'index', $form['Informe']['mes2'], $form['Informe']['year'], $form['Informe']['zona'],$form['Informe']['bimestre'], $form['Informe']['year_origen']));
			} else {
				$this->Session->setFlash(__('The informe could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Informe->read(null, $id);
		}
		
		$this->set(compact('zona','mes','year', 'bimestre', 'year_origen'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for informe', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Informe->delete($id)) {
			$this->Session->setFlash(__('Informe deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Informe was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function mes($bimestre=null, $year_origen=null, $zona=null){
		$usuario_activo = $this->Access->getMy('username');
		//Verificacion de sesion del sistema		
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
		$this->set(compact('zona','bimestre','year_origen'));		
	}
}
?>